package com.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

public class ConstantUtil {
    public final static String loginSession = "loginSession";
    public final static String KAPTCHA_SESSION_KEY = "verifyCode";
    public final static String MESSAGE_SESSION_CODE = "messageCode";
    public final static String EMAIL_SESSION_CODE = "emailCode";
    public final static String FORGOT_USER_SESSION = "forgotUserSession";

    public final static String RECORD_TIME_RANGE = "recordTimeRange";
    public final static String RECORD_TYPE = "recordType";

    public final static String msgCodeSession = "msgCode";


    public static String createWithdrawalNo() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return "W" + uuid;
    }


    public static String createDepositNo() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return "D" + uuid;
    }


    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个IP值，第一个为真实IP。
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        } else {
            return request.getRemoteAddr();
        }
    }

    public static String createRandomVcode(){
        String vcode = "";
        for (int i = 0; i < 4; i++) {
            vcode = vcode + (int)(Math.random() * 9);
        }
        return vcode;
    }

}
